/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.pojo.AttributeType;
import java.util.ArrayList;
import java.util.List;
import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.config.ClientConfig;
import java.io.Serializable;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * Jersey REST client generated for REST resource:AtrributeClass
 * [/attribute]<br>
 * USAGE:
 * <pre>
 *        AttributeTypeService client = new AttributeTypeService();
 *        Object response = client.XXX(...);
 *        // do whatever with response
 *        client.close();
 * </pre>
 *
 * @author U
 */
public class AttributeTypeService implements Serializable {

    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "http://localhost:48651/GPTRP/rest";

    public AttributeTypeService() {
        ClientConfig config = new DefaultClientConfig();
        client = Client.create(config);
        webResource = client.resource(BASE_URI).path("/attribute");

    }

    public List<AttributeType> getAllAttribute() throws JSONException {

        WebResource resource = webResource;
        resource = resource.path("/all");
        JSONArray array = resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(JSONArray.class);
        List<AttributeType> attributetypes = new ArrayList<AttributeType>();
        for (int i = 0; i < array.length(); i++) {
            AttributeType attributeType = new AttributeType();
            JSONObject jSONObject = array.getJSONObject(i);
            attributeType.setId(jSONObject.getInt("id"));
            attributeType.setName(jSONObject.getString("name"));
            attributeType.setIsNumber(jSONObject.getBoolean("isnumber"));
            attributetypes.add(attributeType);
        }
        return attributetypes;
    }

    public void RemoveAttribute(int id) {
        WebResource resource = webResource;
        resource = resource.path("/remove");
       resource.path("/" + id).get(String.class);
       
    }

    public String UpdateAttribute(int id, String name, boolean isnumber) {
        WebResource resource = webResource;
        resource = resource.path("/update");
        String x = resource.path("/" + id + "/" + name + "/" + isnumber).accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
        return x;
    }

    public String insertNewAttribute(AttributeType attribute) {
        WebResource resource = webResource;
        resource = resource.path("/add");
        String x = resource.path("/" + attribute.getId() + "/" + attribute.getName() + "/" + attribute.getIsNumber()).accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
        return x;
    }
   
    public void close() {
        client.destroy();
    }
}
